﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace QuanLySimDienThoai
{
    public partial class frmQuanLySim : Form
    {
        SqlConnection m_sqlConnect;
        SqlCommand m_sqlCommand;
        DataTable m_DataTableNhaMang;
        DataTable m_dataTableSim;
        DataSet m_DataSet;
        DataSet m_dataSetSim;
        SqlDataAdapter m_sqldataAdapter;
        private string SoSim;
        public frmQuanLySim()
        {
            InitializeComponent();
            m_sqlConnect = new SqlConnection("Server=ITQUANGNAM\\SQLEXPRESS;Integrated security=SSPI;database=QuanLySimDT");
            m_DataSet = new DataSet();
            m_dataSetSim = new DataSet();
            try
            {
                m_sqlConnect.Open();

            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi mở kết nối:" + ex.ToString());
            }
            
            
        }

        private void frmQuanLySim_Load(object sender, EventArgs e)
        {
            string strSelect = "select distinct TenNhaMang from NhaMang"; 
            m_sqldataAdapter = new SqlDataAdapter(strSelect, m_sqlConnect);
            m_DataSet.Clear();
            try
            {
                m_sqldataAdapter.Fill(m_DataSet);
                m_DataTableNhaMang = m_DataSet.Tables[0];
            }
            catch (Exception ex) { MessageBox.Show(ex.ToString()); }
            if (m_DataTableNhaMang != null)
            {
                foreach (DataRow row in m_DataTableNhaMang.Rows)
                {
                    cbxNhaMang.Items.Add(row[0].ToString());
                }
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            DialogResult resul = MessageBox.Show("Thoát khỏi chương trình quản lý?", "Quản Lý Sim - Thoát", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (resul == DialogResult.Yes)
            {
                this.Dispose();
            }
        }

        private void btnThemSim_Click(object sender, EventArgs e)
        {
            string strNgayThem = DateTime.Now.ToShortDateString();
            if (cbxNhaMang.Text == "")
            {
                MessageBox.Show("Chưa chọn nhà mạng!");
            }
            else
            {
                m_sqlCommand = new SqlCommand("insert into Sim values('" + txtSoSim.Text + "','" + cbxNhaMang.Text + "','" + txtGia.Text + "','" + strNgayThem + "')", m_sqlConnect);
                try
                {
                    m_sqlCommand.ExecuteNonQuery();
                    MessageBox.Show("Đã thêm sim!");
                }
                catch (Exception ex) { MessageBox.Show("Không thể thêm sim:" + ex.ToString()); }
            }
        }

        private void btnThemNhaMang_Click(object sender, EventArgs e)
        {
            m_sqlCommand = new SqlCommand("insert into NhaMang values('" + txtTenNhaMang.Text + "','" + txtDauSo.Text + "')", m_sqlConnect);
            try
            {
                m_sqlCommand.ExecuteNonQuery();
                MessageBox.Show("Đã thêm nhà mạng!");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        public void btnXemDanhSach_Click(object sender, EventArgs e)
        {
            m_sqlCommand = new SqlCommand("select * from Sim", m_sqlConnect);
            m_dataSetSim.Clear();
            m_sqldataAdapter.SelectCommand = m_sqlCommand;
            try
            {
                m_sqldataAdapter.Fill(m_dataSetSim);
                m_dataTableSim = m_dataSetSim.Tables[0];
                dgvwDanhSachSim.DataSource = m_dataTableSim.DefaultView;
            }
            catch (Exception ex) { MessageBox.Show(ex.ToString()); }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            string str_Delete = "Delete from Sim where SoSim = '" + SoSim + "'";
            m_sqlCommand = new SqlCommand(str_Delete, m_sqlConnect);
            try
            {
                m_sqlCommand.ExecuteNonQuery();
                MessageBox.Show("Da xoa");
                btnXemDanhSach_Click(sender, e);
            }
            catch
            {
                MessageBox.Show("ko xoa duoc");
            }
        }

        private void dgvwDanhSachSim_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                SoSim = dgvwDanhSachSim.SelectedRows[0].Cells[0].Value.ToString();
            }
            catch
            {
            }
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            try
            {
                SuaSim Sua = new SuaSim();
                Sua.SOSIM = dgvwDanhSachSim.SelectedRows[0].Cells[0].Value.ToString();
                Sua.NHAMANG = dgvwDanhSachSim.SelectedRows[0].Cells[1].Value.ToString();
                Sua.GIA = dgvwDanhSachSim.SelectedRows[0].Cells[2].Value.ToString();
                Sua.NGAYTHEM = dgvwDanhSachSim.SelectedRows[0].Cells[3].Value.ToString();
                Sua.ShowDialog();
            }
            catch
            {
                MessageBox.Show("bạn chưa chọn sim cần sửa!!!!");
            }
            btnXemDanhSach_Click(sender, e);
        }
    }
}